package com.demo529;

import java.util.*;
import java.util.InputMismatchException;

public class ViewMain {
    public static void main(String[] args) {
        boolean flag = true;
        TestMap testMap = new TestMap();
        Map<String, Emp> empMap = new HashMap<>();

        while(flag) {
            menu();
            Scanner scanner = new Scanner(System.in);

            try {
                int n = scanner.nextInt();
                scanner.nextLine(); // 清除输入缓冲

                switch(n) {
                    case 0:
                        flag = false;
                        System.out.println("系统已退出");
                        break;
                    case 1:
                        System.out.println("---添加员工---");
                        System.out.print("请输入员工编号: ");
                        String number = scanner.nextLine();
                        System.out.print("请输入员工姓名: ");
                        String name = scanner.nextLine();
                        System.out.print("请输入员工年龄: ");
                        int age = scanner.nextInt();
                        scanner.nextLine(); // 清除换行符
                        Emp emp = new Emp(number, name, age);
                        testMap.addEmp(empMap, emp);
                        break;
                    case 2:
                        testMap.showAllEmp(empMap);
                        break;
                    case 3:
                        System.out.print("请输入要查询的员工姓名: ");
                        String searchName = scanner.nextLine();
                        testMap.findEmpByName(empMap, searchName);
                        break;
                    case 4:
                        System.out.print("请输入要删除的员工工号: ");
                        String delNumber = scanner.nextLine();
                        testMap.deleteEmp(empMap, delNumber);
                        break;
                    case 5:
                        System.out.print("请输入要查询的员工工号: ");
                        String searchNumber = scanner.nextLine();
                        testMap.findEmpByNumber(empMap, searchNumber);
                        break;
                    case 6:
                        System.out.print("请输入要修改的员工工号: ");
                        String updateNumber = scanner.nextLine();
                        System.out.print("请输入新的年龄: ");
                        int newAge = scanner.nextInt();
                        scanner.nextLine(); // 清除换行符
                        testMap.updateAgeByNumber(empMap, updateNumber, newAge);
                        break;
                    default:
                        System.out.println("无效输入，请重新选择！");
                }
            } catch (InputMismatchException e) {
                System.out.println("错误：请输入数字选项！");
                scanner.nextLine(); // 清除错误输入
            }
        }
    }

    public static void menu() {
        System.out.println("\n=== 员工管理系统 ===");
        System.out.println("0. 退出系统");
        System.out.println("1. 添加员工");
        System.out.println("2. 显示所有员工");
        System.out.println("3. 按姓名查询");
        System.out.println("4. 删除员工");
        System.out.println("5. 按工号查询");
        System.out.println("6. 修改年龄");
        System.out.print("请输入操作编号: ");
    }
}